package com.business.form.service;

import com.business.form.mapper.auto.FormInstanceMapper;
import com.business.form.model.auto.FormInstance;
import com.business.form.model.auto.FormInstanceExample;
import com.business.system.common.base.BaseService;
import com.business.system.common.support.Convert;
import com.business.system.model.custom.Tablepar;
import com.business.system.util.SnowflakeIdWorker;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 表单实例 FormInstanceService
 * @Title: FormInstanceService.java 
 * @Package com.business.form.service 
 * @author LuJingyuan_自动生成
 * @email 674326842@qq.com
 * @date 2020-11-18 13:59:34  
 **/
@Service
public class FormInstanceService implements BaseService<FormInstance, FormInstanceExample>{
	@Autowired
	private FormInstanceMapper formInstanceMapper;
	
      	   	      	      	      	      	      	      	      	      	
	/**
	 * 分页查询
	 * @return
	 */
	 public PageInfo<FormInstance> list(Tablepar tablepar,String name){
	        FormInstanceExample testExample=new FormInstanceExample();
	        testExample.setOrderByClause("id ASC");
	        /*if(name!=null&&!"".equals(name)){
	        	testExample.createCriteria().andCreateTimeLike("%"+name+"%");
	        }*/

	        PageHelper.startPage(tablepar.getPageNum(), tablepar.getPageSize());
	        List<FormInstance> list= formInstanceMapper.selectByExample(testExample);
	        PageInfo<FormInstance> pageInfo = new PageInfo<FormInstance>(list);
	        return  pageInfo;
	 }

	@Override
	public int deleteByPrimaryKey(String ids) {
				
			List<String> lista=Convert.toListStrArray(ids);
			FormInstanceExample example=new FormInstanceExample();
			example.createCriteria().andIdIn(lista);
			return formInstanceMapper.deleteByExample(example);
			
				
	}
	
	
	@Override
	public FormInstance selectByPrimaryKey(String id) {
				
			return formInstanceMapper.selectByPrimaryKey(id);
				
	}

	
	@Override
	public int updateByPrimaryKeySelective(FormInstance record) {
		return formInstanceMapper.updateByPrimaryKeySelective(record);
	}
	
	
	/**
	 * 添加
	 */
	@Override
	public int insertSelective(FormInstance record) {
				
		//添加雪花主键id
		record.setId(SnowflakeIdWorker.getUUID());
			
				
		return formInstanceMapper.insertSelective(record);
	}
	
	
	@Override
	public int updateByExampleSelective(FormInstance record, FormInstanceExample example) {
		
		return formInstanceMapper.updateByExampleSelective(record, example);
	}

	
	@Override
	public int updateByExample(FormInstance record, FormInstanceExample example) {
		
		return formInstanceMapper.updateByExample(record, example);
	}

	@Override
	public List<FormInstance> selectByExample(FormInstanceExample example) {
		
		return formInstanceMapper.selectByExample(example);
	}

	
	@Override
	public long countByExample(FormInstanceExample example) {
		
		return formInstanceMapper.countByExample(example);
	}

	
	@Override
	public int deleteByExample(FormInstanceExample example) {
		
		return formInstanceMapper.deleteByExample(example);
	}
	
	/**
	 * 检查name
	 * @param formInstance
	 * @return
	 */
	public int checkNameUnique(FormInstance formInstance){
		FormInstanceExample example=new FormInstanceExample();
		example.createCriteria().andCreateTimeEqualTo(formInstance.getCreateTime());
		List<FormInstance> list=formInstanceMapper.selectByExample(example);
		return list.size();
	}

	/**
	 * 根据表单id和诉求id查询出对应的表单实例
	 * @param formId
	 * @param appealInfoId
	 * @return
	 */
    public FormInstance seletFormInstance(String formId, String appealInfoId) {
		FormInstanceExample e = new FormInstanceExample();
		FormInstanceExample.Criteria criteria = e.createCriteria();
		criteria.andFormInfoIdEqualTo(formId);
		criteria.andAppealInfoIdEqualTo(appealInfoId);
		List<FormInstance> formInstances = formInstanceMapper.selectByExample(e);
		return formInstances.size()>0 ? formInstances.get(0) : null;
	}
}
